//
//  DTJavascriptCommand.m
//  PersonalManager
//
//  Created by user on 16/8/1.
//  Copyright © 2016年 Haiyun.Qian. All rights reserved.
//

#import "DTJavascriptCommand.h"
#import <Foundation/Foundation.h>
#import "WebViewJavascriptBridge.h"
#import "SBJson.h"

@implementation DTJavascriptCommand

- (instancetype)init: (NSString *)handleName anddata:(NSObject *)data andresponscallback: (WVJBResponseCallback) responseCallback andwebView: (UIWebView *)webView andcontroller: (UIViewController *)controller
{
    self = [super init];
    
    self.handleName = handleName;
    NSArray *handleSplit = [handleName componentsSeparatedByString:@"."];
    self.moduleName = handleSplit.count >= 1 ? handleSplit[0] : nil;
    self.className  = handleSplit.count >= 2 ? handleSplit[1] : nil;
    self.methodName = handleSplit.count >= 3 ? handleSplit[2] : nil;
    self.params     = (NSDictionary *)data;
    self.responseCallback = responseCallback;
    self.webView    = webView;
    self.controller = controller;
    
    return self;
}

-(NSString *) getParamValue: (NSString *) key {
    if (self.params) {
        return self.params[key];
    }else {
        return @"";
    }
}


-(void) cancelResponse: (NSString *)message {
    message =@"";
    
    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"-1",@"errorCode",message,@"errorMessage", nil];
    NSString *stringJSON = [dic JSONRepresentation];

    self.responseCallback(stringJSON);
    self.actionObject = nil;
}

-(void) successRessponse: (NSDictionary *)result {
    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"0",@"errorCode",@"成功",@"errorMessage",result,@"result", nil];
    self.responseCallback([dic JSONRepresentation]);
    self.actionObject = nil;
}
-(void) failedResponse: (int)code andmessage: (NSString *)message {
    
    NSString *errorMessage = @"失败";
    if (message) {
        errorMessage = message;
    }
    NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%d",code],@"errorCode",errorMessage,@"errorMessage", nil];
    NSString *stringJSON = [dic JSONRepresentation];
    self.responseCallback(stringJSON);
    self.actionObject = nil;
}

#pragma mark - UIPopoverPresentationControllerDelegate
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller{
    return UIModalPresentationNone;
}

@end





